import sys
from collections import defaultdict

sys.setrecursionlimit(100000)
n = int(input())
happy = [0]
for _ in range(n):
    happy.append(int(input()))
edges = defaultdict(list)
has_parent = [False] * (n + 1)
for _ in range(n - 1):
    l, k = map(int, input().split())
    edges[k].append(l)
    has_parent[l] = True
maxvalue = -float('inf')
root = 1
for i in range(1, n + 1):
    if not has_parent[i]:
        root = i
        break


def dfs(node: int):
    # if not node: return [0,0]
    global maxvalue
    not_come = 0
    come = happy[node]
    for child in edges[node]:
        c = dfs(child)
        not_come += max(c)
        come += c[1]  # 子不能来
    maxvalue = max(maxvalue, come, not_come)
    return come, not_come


dfs(root)
print(maxvalue)
